package net.vinote.smart.platform.service.handler;

import java.sql.SQLException;

import net.vinote.smart.platform.comm.StringFunction;
import net.vinote.smart.platform.module.database.DBTransaction;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.service.action.TimerTaskAction;
import net.vinote.smart.platform.service.dw.handler.CommonHandler;
import net.vinote.smart.platform.service.dw.handler.HandlerPerimissionAnnotation;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;
import net.vinote.smart.platform.timer.TimerManager;

@HandlerPerimissionAnnotation(
		insert = @Permission(PermissionEnum.SMART_TIME_TASK_INSERT),
		update = @Permission(PermissionEnum.SMART_TIME_TASK_UPDATE),
		delete = @Permission(PermissionEnum.SMART_TIME_TASK_DELETE),
		query = @Permission(PermissionEnum.SMART_TIME_TASK_QUERY))
public class TimerTaskHandler extends CommonHandler {

	@Override
	public void initDisplayForEdit(BizObject bo) {
		if (!StringFunction.isBlank(bo.getAttribute("ID"))) {
			TimerTaskAction action = new TimerTaskAction();
			action.setId(bo.getAttribute("ID"));
			bo.setAttribute("Status", action.getStatus());
		}
	}

	@Override
	protected void beforeDelete(BizObject bo, DBTransaction tx)
			throws SQLException {
		TimerManager.getInstance().cancleTask(bo.getAttribute("id"));
	}

}
